﻿#include "CPdfRptDataPkg.h"
#include "CGlobal.h"
#include "CSqlOper.h"
#include "Public.h"

CPdfRptDataPkg::CPdfRptDataPkg(QObject *parent)
	: QObject(parent)
{

}

CPdfRptDataPkg::~CPdfRptDataPkg()
{

}

bool CPdfRptDataPkg::pkgRestPdfRptData(QDateTime dtSearchStartTime, QDateTime dtSearchEndTime, QString sPdfFolderPath, QString sHttpPrefix, CAllPdfRptDataInfo &dataInfo, QString &sErrMsg)
{
	//纠偏
	QDateTime dtCurTime = QDateTime::currentDateTime();//当前系统时间
	QString sCurrTime = dtCurTime.toString("yyyyMMddhhmmss");
	//创建文件夹
	QString sFolderTemp = sPdfFolderPath + "Result/" + "Rectify" + "/" + QDateTime::currentDateTime().toString("yyyyMM") + "/";//文件夹+Rectify+年月
	QDir folder(sFolderTemp);// 创建QDir对象
	if (!folder.exists())
	{
		// 文件夹不存在，则创建
		folder.mkpath(".");
	}
	//输出路径
	QString sOutPath = sFolderTemp + sCurrTime + A2T("跑偏检测报告.pdf");
	dataInfo.rectifyDataInfo.m_sOutputPdfFolder = sOutPath;
	QString sMsgRectify = "";
	QVector<QSqlRecord> vecRecordRectify;
	CSqlOper::instance().selectRecRetInfo(vecRecordRectify, dtSearchStartTime.toString("yyyy-MM-dd hh:mm:ss"), dtSearchEndTime.toString("yyyy-MM-dd hh:mm:ss"), sMsgRectify);
	//dataInfo.rectifyDataInfo.m_nAlmCount = vecRecordRectify.size();
	dataInfo.rectifyDataInfo.m_sComment = A2T("空");
	dataInfo.rectifyDataInfo.m_sDetTime = dtSearchEndTime.toString("yyyy-MM-dd");
	dataInfo.rectifyDataInfo.m_sDetStartTime = dtSearchStartTime.toString("hh:mm:ss");
	dataInfo.rectifyDataInfo.m_sDetEndTime = dtSearchEndTime.toString("hh:mm:ss");
	for (int i = 0; i < vecRecordRectify.size(); i++)
	{
		QDateTime dtAlmNow = vecRecordRectify[i].value("INSERT_TIME").toDateTime();


		if (1 == vecRecordRectify[i].value("ALARM_TYPE").toInt())//报警才触发筛选
		{
			//需要根据配置的筛选时间筛选,比如60分钟之内只能有一条报警
			if (0 == i)//第一个报警是需要的
			{
				m_dtPrevAlm = dtAlmNow;
				dataInfo.rectifyDataInfo.m_nAlmCount += 1;
			}
			else//往后的报警进行比较
			{
				// 计算时间差（秒）
				int nSecDif = dtAlmNow.secsTo(m_dtPrevAlm);
				if (nSecDif < 3600)//指定分钟(比如60分钟)之内
				{
					continue;//本次报警直接过滤
				}
				else//超过时间，可以取新的报警了
				{
					m_dtPrevAlm = dtAlmNow;
					dataInfo.rectifyDataInfo.m_nAlmCount += 1;
				}

			}
		}

		SPdfRecSglAlmInfo pdfAlmInfo;
		pdfAlmInfo.m_sPictureUrl = vecRecordRectify[i].value("HTTP_PIC_URL").toString();
		pdfAlmInfo.m_sBeltName = vecRecordRectify[i].value("BELT_NAME").toString();
		pdfAlmInfo.m_sRecPointName = vecRecordRectify[i].value("POINT_NAME").toString();
		pdfAlmInfo.m_sRecStartTime = vecRecordRectify[i].value("RECTIFY_START_TIME").toDateTime().toString("yyyy-MM-dd HH:mm:ss");
		pdfAlmInfo.m_sRecEndTime = vecRecordRectify[i].value("RECTIFY_END_TIME").toDateTime().toString("yyyy-MM-dd HH:mm:ss");
		pdfAlmInfo.m_nAlmType = vecRecordRectify[i].value("ALARM_TYPE").toInt();

		pdfAlmInfo.m_sAlmDesc = vecRecordRectify[i].value("ALARM_DESC").toString();
		pdfAlmInfo.m_dLeftCurVal = vecRecordRectify[i].value("LEFT_CURREENT_VALUE").toDouble();
		pdfAlmInfo.m_dRightCurVal = vecRecordRectify[i].value("RIGHT_CURREENT_VALUE").toDouble();
		pdfAlmInfo.m_nReaSetValOne = vecRecordRectify[i].value("REALIGN_SET_VALUE").toInt();
		pdfAlmInfo.m_nReaSetValTwo = vecRecordRectify[i].value("REALIGN_SET_VALUE_TWO").toInt();
		pdfAlmInfo.m_dLeftAvgVal = vecRecordRectify[i].value("LEFT_AVG_VALUE").toDouble();
		pdfAlmInfo.m_dRightAvgVal = vecRecordRectify[i].value("RIGHT_AVG_VALUE").toDouble();

		dataInfo.rectifyDataInfo.m_vecFormRecAlmInfo.push_back(pdfAlmInfo);
	}
	//组包完成后，将生成报告的记录插入到数据库中
	{
		//去查询报警记录表
		QVector<QSqlRecord> vecInfo;
		QString sErrMsg = "";
		QString sPath = sOutPath.remove(sPdfFolderPath);
		QString sPdfPathUrl = sHttpPrefix + sPath;
		QString sRobPdfPathUrl = sPdfPathUrl.replace("#", "%23");//URL路径
		CSqlOper::instance().insertResult(createGuid(), 3, sCurrTime + A2T("跑偏检测报告.pdf"), dtSearchEndTime, dataInfo.rectifyDataInfo.m_sOutputPdfFolder, sRobPdfPathUrl, sErrMsg);
	}
	return true;
}
